package com.tjshop.android;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.tjshop.android.adapter.activity_adapter;
import com.tjshop.android.adapter.shopCoupon_adapter;
import com.tjshop.android.mcv.GlobeVars;
import com.tjshop.android.model.activityInfo;
import com.tjshop.android.util.HttpUtil;
import com.tjshop.android.util.MD5Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class Discover02EventActivity extends AbsBaseActionBarActivity implements
		View.OnClickListener, Handler.Callback {
	private Context mContext;
	private Handler handler;
	private ArrayList<activityInfo> ActivityInfos = new ArrayList<activityInfo>();
	private activity_adapter mSimpleAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_discover_02_event);
		mContext = this;
		handler = new Handler(this);

		ListView listView = (ListView) findViewById(R.id.nearby_activity_list);
		mSimpleAdapter = new activity_adapter(getApplicationContext(),
				ActivityInfos, getLayoutInflater());
		listView.setAdapter(mSimpleAdapter);
		// getActivityList(GlobeVars.latitude+"",GlobeVars.longitude+"",0+"");
		getActivityList("113.806956", "34.021977", 0 + "");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.activity_discover_event, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		// case R.id.action_discover_event_test:
		// Toast.makeText(this, "You clicked an options item",
		// Toast.LENGTH_SHORT).show();
		// return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// case R.id.event_test_01:
		// startActivity(new Intent(this, EventDetailActivity.class));
		// break;

		default:
			break;
		}
	}

	void getActivityList(String Lng, String Lat, String Offset) {
		String md5 = MD5Utils.md5(Lng + Lat + Offset + AppConfig.SignKey);
		final String param = "lng=" + Lng + "&lat=" + Lat + "&offset=" + Offset
				+ "&sign=" + md5;
		new Thread(new Runnable() {
			@Override
			public void run() {
				Message message = new Message();
				String ret = HttpUtil.sendPost(AppConfig.NearbyActivityUrl,
						param);
				if (!ret.equals("")) {
					try {
						JSONObject json = new JSONObject(ret);
						if (json.getInt("code") == 0) {
							JSONObject data = json.getJSONObject("data");
							JSONArray list = data.getJSONArray("list");
							for (int i = 0; i < list.length(); i++) {
								JSONObject item = list.getJSONObject(i);
								activityInfo info = new activityInfo();
								info.setEvent_id(item.getString("event_id"));
								info.setTitle(item.getString("title"));
								info.setIcon(item.getString("icon"));
								info.setDesc(item.getString("desc"));
								info.setDistance(item.getString("distance"));
								info.setStart_time(item.getString("start_time"));
								info.setEnd_time(item.getString("end_time"));
								info.setTotal_user(item.getString("total_user"));
								info.setHeadcount(item.getString("headcount"));
								info.setIsSign(item.getInt("isSign"));
								info.setEventSign(item.getInt("eventSign"));
								ActivityInfos.add(info);
							}
							message.what = 1;
							handler.sendMessage(message);
						} else {
							message.what = 2;
							handler.sendMessage(message);
						}
					} catch (JSONException e) {
						message.what = 2;
						handler.sendMessage(message);
						e.printStackTrace();
					}
				}

			}
		}).start();
	}

	@Override
	public boolean handleMessage(Message message) {
		switch (message.what) {
		case 1:
			Toast.makeText(mContext, "获取附近活动成功！", Toast.LENGTH_SHORT).show();
			mSimpleAdapter.notifyDataSetChanged();
			break;
		case 2:
			Toast.makeText(mContext, "获取附近活动失败!", Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}
		return false;
	}
}
